<template>
  <el-dialog :title="isEdit?'编辑货权调账':'新增货权调账'"
             :visible.sync="dialogForm"
             width="1100px">
    <el-form :inline="true"
             ref="unifyAccountForm"
             :model="unifyform"
             :rules="rules"
             class="add-form"
             label-width="100px">
      <el-row>
        <el-col :span="7">
          <el-form-item label="货主单位"
                        label-width="78px"
                        prop="search_EQ_cargoOwnerId">
            <auto-search-name
                              placeholder="请选择"
                              @selectId="v=>unifyform.search_EQ_cargoOwnerId=v"
                              @selectName="v=>unifyform.search_EQ_cargoOwnerName=v"
                              :name="unifyform.search_EQ_cargoOwnerName"></auto-search-name>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="存单号">
            <el-input type="text" v-model="unifyform.stockBillNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="跟踪号">
            <el-input type="text" v-model="unifyform.barcode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="品名"
                        label-width="78px">
            <el-select v-model="unifyform.search_EQ_cargoName"
                       placeholder="请选择"
                       clearable
                       filterable
                       @change="handleCargoChange">
              <el-option v-for="item in cargoNameList"
                         :key="item.cargoName"
                         :label="item.cargoName"
                         :value="item.cargoName"></el-option>
            </el-select>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="生产厂家">
            <el-select v-model="unifyform.search_EQ_madeBy"
                        placeholder="请选择">
              <el-option v-for="(item,index) in pmLIst"
                        :key="index"
                        :label="item.cargoInfo.madeBy"
                        :value="item.cargoInfo.madeBy">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="材质">
            <el-select v-model="unifyform.search_EQ_material"
                        placeholder="请选择">
              <el-option v-for="(item,index) in pmLIst"
                        :key="index"
                        :label="item.cargoInfo.material"
                        :value="item.cargoInfo.material">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="计量方式" label-width="78px">
            <el-select v-model="unifyform.search_EQ_measureWay"
                        placeholder="请选择">
              <el-option v-for="[key, val] in _dictMeterStyle"
                        :key="key"
                        :label="val"
                        :value="key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5"
                class="search-width8">
          <el-form-item label="规格"
                        label-width="78px">
            <el-col :span="8">
              <el-form-item prop="search_EQ_normOne"
                            class="gg-input">
                <el-select v-model="unifyform.search_EQ_normOne"
                           style="width: 100px;">
                  <el-option v-for="(item,index) in pmLIst"
                             :key="index"
                             :label="item.cargoInfo.normOne"
                             :value="item.cargoInfo.normOne">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="search_EQ_normTwo"
                            class="gg-input">
                <el-select v-model="unifyform.search_EQ_normTwo"
                           style="width: 100px;">
                  <el-option v-for="(item,index) in pmLIst"
                             :key="index"
                             :label="item.cargoInfo.normTwo"
                             :value="item.cargoInfo.normTwo">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="search_EQ_normThree"
                            class="gg-input">
                <el-select v-model="unifyform.search_EQ_normThree"
                           style="width: 100px;">
                  <el-option v-for="(item,index) in pmLIst"
                             :key="index"
                             :label="item.cargoInfo.normThree"
                             :value="item.cargoInfo.normThree">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="管理方式" label-width="78px">
            <el-select v-model="unifyform.search_EQ_manageWay"
                        placeholder="请选择" :disabled="true">
              <el-option v-for="[key, val] in _dictManagement"
                        :key="key"
                        :label="val"
                        :value="key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3"
                style="width: 190px;">
          <el-button type="primary"
                      icon="el-icon-search"
                      @click="searchunifyForm"
                      class="ml-80">查 询</el-button>
          <el-button formVisible="true"
                      icon="el-icon-refresh">
            重置
          </el-button>
        </el-col>
      </el-row>
      <h3>调账信息</h3>
      <hr>
      <el-row class="tsinput">
        <el-col :span="5">
          <el-radio v-model="unifyform.adjustWay" v-for="[key, val] in _dictAdjustWay"
                        :key="key"
                        :label="val"
                        :value="key"></el-radio>
        </el-col>
        <el-col :span="5">
          <el-form-item label="调整件数">
            <el-input v-model="unifyform.num"  style="width: 100px;" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="调整重量">
            <el-input v-model="unifyform.weight"  style="width: 100px;" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>   
        <el-col :span="8">
          <el-form-item label="调整原因">
            <el-input class="result" v-model="unifyform.remark"  style="width: 200px;" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table ref="singleTable"
                    :data="unifyAccount"
                    border
                    highlight-current-row>
            <el-table-column
                  width="40">
                <template slot-scope='scope'>
                  <el-radio v-model="scopeRadio" :label="scope.$index" @change.native="handleSelectionChange(scope.row)"></el-radio>
                </template>
            </el-table-column>
            <el-table-column label="跟踪号"
                             min-width="100"
                             prop="barcode">
            </el-table-column>
            <el-table-column label="物资ID"
                             min-width="100"
                             prop="inventoryId">
            </el-table-column>
            <el-table-column label="存单号"
                             min-width="100"
                             prop="stockBillNo">
            </el-table-column>
            <el-table-column label="货主"
                             min-width="100"
                             prop="cargoOwnerName">
            </el-table-column>
            <el-table-column label="品名"
                             min-width="100"
                             prop="cargoName">
            </el-table-column>
            <el-table-column label="管理方式"
                             min-width="100"
                             prop="manageWay">
            </el-table-column>
            <el-table-column label="规格"
                             min-width="100">
                <template slot-scope='scope'>
                    {{''+scope.row.normOne +'*'+ scope.row.normTwo +'*'+ scope.row.normThree }}
                </template>
            </el-table-column>
            <el-table-column label="材质"
                             min-width="100"
                             prop="material">
            </el-table-column>
            <el-table-column label="生产厂家"
                             min-width="100"
                             prop="madeBy">
            </el-table-column>
            <el-table-column label="计量方式"
                             min-width="100"
                             prop="measureWay">
            </el-table-column>
            <el-table-column label="上账重量"
                             min-width="100"
                             prop="balanceWeight">
            </el-table-column>
            <el-table-column label="验收重量"
                             min-width="100"
                             prop="balanceNum">
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer"
         class="dialog-footer">
      <el-button @click="dialogForm = false">取 消</el-button>
      <el-button type="primary"
                 @click="submitAdjuctPage('1')"
                 :loading="$store.getters.loadingBtn">保 存</el-button>
      <el-button type="primary"
                 @click="submitAdjuctPage('2')"
                 :loading="$store.getters.loadingBtn">提 交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getCargoTypeList } from '@/api/baseResource/baseResource' //货物种类的下拉菜单
// import { getCargoNameList } from '@/api/baseResource/baseResource' //品名的下拉菜单
import { addThings, updateThings } from '@/api/things/things'
import autoSearchDic from '@/components/autocomplete/autoSearchDic'
import autoSearchName from '@/components/autocomplete/autoSearchName' //货主组件
import { maxLength, sortLength } from '@/utils/rules'
import { findViceAccount, 
  getCargoNameList,
  getPiecesCargo,
  getAdjustEdit,
  getAdjustAdd
} from '@/api/things/accountPageList'

export default {
  name: 'unifyAccountForm',
  components: { autoSearchDic, autoSearchName },
  data() {
    return {
      unifyform: this.clearForm(),
      dialogForm: false,
      unifyAccount: [],
      cargoNameList: [],
      pmLIst: [], //接口获取的货物属性数据
      rules: {
        // lineNo: [
        //   { required: true, message: '请输入专运线', trigger: 'blur' },
        //   sortLength
        // ],
        // orgId: [{ required: true, message: '请选择单位', trigger: 'change' }],
        // deptId: [{ required: true, message: '请选择部门', trigger: 'change' }]
        // remark: [maxLength]
      },
      component: '',
      condition: {}, // 查询条件
      pageSearch: findViceAccount, // 分页查询api接口
      isEdit: false,
      maxLot: 0,
      selectThingsType: [],
      selectGoodsType: [],
      selectGroductName: [],
      inoutradio: null,
      scopeRadio: null,
    }
  },
  mounted() {
    getCargoTypeList().then(res => {
      this.selectThingsType = res.data.aaData
    })
    //获取货名
    getCargoNameList(this.$store.state.user.currentUser.org.id).then(res => {
      this.cargoNameList = res.data.aaData
    })
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name,
        id: '',
        accountpageId:'',
        search_EQ_accountPageType: '', //账页类型
        search_EQ_cargoName: '', //品名
        search_EQ_endTime: '', //建账结束时间
        search_EQ_startTime: '', //建账开始时间
        search_EQ_Time: '', //建账时间
        search_EQ_madeBy: '', //生产厂家
        search_EQ_manageWay: 'SPECIFY', //管理方式
        search_EQ_material: '', //材质
        search_EQ_measureWay: '', //计量方式
        search_EQ_normOne: '', //规格
        search_EQ_normThree: '',
        search_EQ_normTwo: '',
        search_EQ_warehouseCode: '', //所属仓库
        search_LIKE_accountpageNo: '', //账页号
        search_EQ_cargoOwnerCode: '', //货主单位
        search_EQ_cargoOwnerId: '', //货主单位
        sortType: '', //排序
        balanceWeight: '',
        balanceNum: '',
        adjustWay: '',
        weight: '',
        num: '',
        remark: '',
        barcode: '',
        stockBillNo: ''
      }
    },
    loadData() {
      this.$refs.unifyform.loadData()
    },
    selectChange(val) {
      // 获取货物name
      // if (this.form.cargoTypeCode === val) {
      //   this.form.cargoTypeName = val
      // }
      // 品名
      // this.selectGroductName = []
      // for (let i = 0; i < this.selectGoodsType.length; i++) {
      //   if (this.selectGoodsType[i].cargoTypeCode === this.form.cargoTypeCode) {
      //     let obj = new Object()
      //     obj.cargoCode = this.selectGoodsType[i].cargoCode
      //     obj.cargoName = this.selectGoodsType[i].cargoName
      //     this.selectGroductName.push(obj)
      //   }
      // }
    },
    searchunifyForm() {
      let that = this
      let param = {
        search_EQ_accountPageType: this.unifyform.search_EQ_accountPageType, //账页类型
        search_EQ_cargoName: this.unifyform.search_EQ_cargoName,  //品名
        search_EQ_madeBy: this.unifyform.search_EQ_madeBy,  //生产厂家
        search_EQ_material: this.unifyform.search_EQ_material,   //材质
        search_EQ_measureWay: this.unifyform.search_EQ_measureWay,  //计量方式
        search_EQ_normOne: this.unifyform.search_EQ_normOne, //规格
        search_EQ_normThree: this.unifyform.search_EQ_normThree,
        search_EQ_normTwo: this.unifyform.search_EQ_normTwo,
        search_LIKE_accountpageNo: this.unifyform.search_LIKE_accountpageNo,  //账页号
        sortType: '',  //排序
        search_EQ_cargoOwnerId: this.unifyform.search_EQ_cargoOwnerId,
        search_EQ_manageWay: 'SPECIFY',
        iDisplayLength: 10,
        iDisplayStart: 0,
        sortType: "-createDateTime",
      }
      let pam = {}
      for(let i in param){
        if(param[i]){
          pam[i] = param[i]
        }
      }
      findViceAccount(pam).then(function(res) {
        that.unifyAccount = res.data.aaData
        for(var i=0;i<that.unifyAccount.length;i++){
          that.unifyform.balanceWeight = that.unifyAccount[i].balanceWeight
          that.unifyform.balanceNum = that.unifyAccount[i].balanceNum
        }
      })
    },
    handleCargoChange(status) {
      let that = this
      let pam = {}
      for(let i in that.unifyform){
        if(that.unifyform[i]){
          pam[i] = that.unifyform[i]
        }
      }
      getPiecesCargo(pam).then(function(res) {
        that.pmLIst = res.data.aaData
        if(that.pmLIst.length == 1) {
          that.unifyform.search_EQ_normOne = that.pmLIst[0].cargoInfo.normOne
          that.unifyform.search_EQ_normTwo = that.pmLIst[0].cargoInfo.normTwo
          that.unifyform.search_EQ_normThree = that.pmLIst[0].cargoInfo.normThree
          that.unifyform.search_EQ_madeBy = that.pmLIst[0].cargoInfo.madeBy
          that.unifyform.search_EQ_material = that.pmLIst[0].cargoInfo.material
          that.unifyform.search_EQ_measureWay = that.pmLIst[0].cargoInfo.measureWay
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      this.multipleSelection.push(val);
      this.unifyform.accountpageId = val.id;
    },
    //提交保存
    submitAdjuctPage(type) {
      if(!this.unifyform.adjustWay) {
        this.$message({
          message: '请选择调账类型',
          type: 'error'
        })
        return false;
      }
      if(!this.unifyform.num) {
        this.$message({
          message: '请输入调账件数',
          type: 'error'
        })
        return false;
      } 
      if(!this.unifyform.weight) {
        this.$message({
          message: '请输入调账重量',
          type: 'error'
        })
        return false;
      }
      if(!this.multipleSelection || this.multipleSelection.length == 0) {
        this.$message({
          message: '请选择一条账页',
          type: 'error'
        })
        return false;
      }
      let param = {
        "accountPageType": this.unifyform.search_EQ_accountPageType,
        "accountpageNo": this.unifyform.search_LIKE_accountpageNo,
        "adjustWay": this.unifyform.adjustWay,
        "balanceNum": this.unifyform.balanceNum,
        "balanceWeight": this.unifyform.balanceWeight,
        "cargoName": this.unifyform.search_EQ_cargoName,
        "cargoOwnerCode": this.unifyform.search_EQ_cargoOwnerCode,
        "cargoOwnerName": this.unifyform.search_EQ_cargoOwnerName,
        "madeBy": this.unifyform.search_EQ_madeBy,
        "manageWay": "UNIFIED",
        "id": this.unifyform.id,
        "accountpageId": this.unifyform.accountpageId,
        "material": this.unifyform.search_EQ_material,
        "measureWay": this.unifyform.search_EQ_measureWay,
        "normOne": this.unifyform.search_EQ_normOne,
        "normThree": this.unifyform.search_EQ_normThree,
        "normTwo": this.unifyform.search_EQ_normTwo,
        "num": this.unifyform.num,
        "remark": this.unifyform.remark,
        "weight": this.unifyform.weight,
        "yopenedWeight": 0
      }
      let pam = {}
      for(let i in param){
        if(param[i] != ''){
          pam[i] = param[i]
        }
      }
      if(pam.adjustWay == '调整入库') {
        pam.adjustWay = 'TZRK'
      }else{
        pam.adjustWay = 'TZCK'
      }
      pam.num = parseInt(pam.num);
      pam.weight = parseInt(pam.weight);
      this.isEdit ? this.handleToEdit(type,pam) : this.handleToAdd(type,pam)
    },
    // 新增
    handleToAdd(type,pam) {
      getAdjustAdd(type,pam).then(res => {
        this.$message({ message: '新增成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    //获取id
    getId(val) {
      this.unifyform.id = val
    },
    // 编辑
    handleToEdit(type,pam) {
      getAdjustEdit(pam.id,type,pam).then(res => {
        this.$message({ message: '编辑成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    }
  },
  watch: {
    dialogForm(bool) {
      if (!bool) {
        this.$refs.unifyAccountForm.resetFields()
        this.isEdit = false
        this.unifyform = this.clearForm()
        this.$emit('clear')
      }
    }
  }
}
</script>

<style>
.tsinput {
  margin: 10px 0;
}
.tsinput .el-form-item__content .el-input__inner {
  width: 100px;
}
.tsinput .el-form-item__content .result .el-input__inner {
  width: 220px;
}
</style>
